<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>
<script>
  var arr = function (nums, value) {
    const count = new Array(value).fill(0);
    nums.forEach(val => count[(val % value + value) % value]++);
    let now = 0;
    while (1) {
      let t = now % value;
      if (count[t] > 0) count[t]--;
      else return now;
      now++;
    }
  };
  console.log(arr(nums = [1, -10, 7, 13, 6, 8], value = 5));
</script>

</html>